package shop

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"github.com/hwUltra/fb-tools/gormx"

	"github.com/zeromicro/go-zero/core/logx"
)

type BrandPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// Brand列表
func NewBrandPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BrandPageLogic {
	return &BrandPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BrandPageLogic) BrandPage(in *types.BrandPageReq) (resp *types.BrandPageResp, err error) {
	m := model.PmsBrandModel{}

	total := int64(0)
	if err := l.svcCtx.GormClient.GormDb.Model(m).
		Scopes(
			m.WithShopId(in.ShopId),
			m.WithKeywords(in.Keywords)).
		Count(&total).Error; err != nil {
		return nil, err
	}

	list := make([]*types.PmsBrand, 0)

	if total > 0 {
		l.svcCtx.GormClient.GormDb.Model(m).
			Scopes(
				gormx.Paginate(int(in.PageNum), int(in.PageSize)),
				m.WithShopId(in.ShopId),
				m.WithKeywords(in.Keywords)).
			Order("id asc").Find(&list)

	}

	return &types.BrandPageResp{List: list, Total: total}, nil
}
